import { ref,onMounted } from 'vue'
const { ipcRenderer, remote } = require("electron");
const { Menu,MenuItem } = remote

export const getParentNode = (node,parrentClassName) => {
    let current = node
    while(current!=null){
        if(current.classList){
            if(current.classList.contains(parrentClassName)){
                return current
            }
            current = current.parentNode
        }else{
            current = null
        } 
    }
    return false
}
/**
 * 
 * @param {*} itemArr  [
      {
        label: "重命名",
        click: () => {
          console.log(222);
        },
      },
    ]
@param {*} itemArr targetSelectar 包括的元素
 */
export const initMenu = (itemArr,targetSelectar) => {
    const ele = ref(null)
    onMounted(() => {
        const menu = new Menu()
        itemArr.forEach(it => {
            menu.append(new MenuItem(it))
        });
        const handleContextMenu = (e) => {
            e.preventDefault();
            if(document.querySelector(targetSelectar).contains(e.target)){
                ele.value = e.target
                menu.popup({window:remote.getCurrentWindow()}) // 将此菜单作为 browserWindow 中的上下文菜单弹出
            }
        }
        window.addEventListener(
            "contextmenu",
            handleContextMenu,
            false
        );

    })
    return ele
}


